// 定义题库
const questions = [
    {
        question: "以下哪个是C++的正确文件后缀？",
        options: [".cpp", ".java", ".py", ".html"],
        correct: 0
    },
    {
        question: "以下哪个关键字用于动态内存分配？",
        options: ["malloc", "new", "allocate", "delete"],
        correct: 1
    },
    {
        question: "在C++中，`std::cout`用于什么？",
        options: ["输入数据", "输出数据", "清除数据", "排序数据"],
        correct: 1
    },
    {
        question: "C++中的`#include`指令的作用是什么？",
        options: ["导入库文件", "定义变量", "循环执行", "条件判断"],
        correct: 0
    },
    {
        question: "以下哪个容器是C++ STL的一部分？",
        options: ["ArrayList", "Vector", "Dictionary", "Set"],
        correct: 1
    },
    {
        question: "C++中可以使用以下哪个关键字定义常量？",
        options: ["define", "constant", "const", "let"],
        correct: 2
    },
    {
        question: "在C++中，类和结构体的主要区别是什么？",
        options: ["类是静态的，结构体是动态的", "类的成员默认是私有的，结构体是公有的", "类需要手动释放内存", "两者完全相同"],
        correct: 1
    },
    {
        question: "以下哪个不是C++中的访问控制修饰符？",
        options: ["public", "private", "protected", "global"],
        correct: 3
    },
    {
        question: "以下哪个选项用于结束C++程序的执行？",
        options: ["terminate()", "return", "exit()", "close()"],
        correct: 2
    },
    {
        question: "C++中，函数重载的主要条件是什么？",
        options: ["函数名相同，参数数量或类型不同", "函数名不同，参数相同", "返回值不同", "函数所在文件不同"],
        correct: 0
    }
];

let currentIndex = 0;
let totalQuestionsPerRound = 5;
let results = [];

// DOM 元素
const questionBox = document.getElementById("question-box");
const questionText = document.getElementById("question-text");
const answerOptions = document.getElementById("answer-options");
const submitButton = document.getElementById("submit-button");
const resultBox = document.getElementById("result-box");
const resultList = document.getElementById("result-list");
const continueButton = document.getElementById("continue-button");
const backButton = document.getElementById("back-button");

// 加载题目
function loadQuestions() {
    const end = Math.min(currentIndex + totalQuestionsPerRound, questions.length);
    for (let i = currentIndex; i < end; i++) {
        const question = questions[i];
        const questionItem = {
            index: i,
            question: question.question,
            userAnswer: null,
            correctAnswer: question.correct
        };
        results.push(questionItem);
    }
    renderQuestion(0);
}

// 渲染单道题目
function renderQuestion(index) {
    const currentQuestion = results[index];
    questionText.textContent = currentQuestion.question;
    answerOptions.innerHTML = "";

    questions[currentQuestion.index].options.forEach((option, idx) => {
        const li = document.createElement("li");
        li.textContent = option;
        li.dataset.index = idx;
        li.addEventListener("click", () => handleAnswerSelect(index, idx));
        answerOptions.appendChild(li);
    });

    submitButton.classList.add("hidden");
}

// 处理答案选择
function handleAnswerSelect(questionIndex, answerIndex) {
    results[questionIndex].userAnswer = answerIndex;
    if (questionIndex < results.length - 1) {
        renderQuestion(questionIndex + 1);
    } else {
        submitButton.classList.remove("hidden");
    }
}

// 显示结果
function showResults() {
    questionBox.classList.add("hidden");
    submitButton.classList.add("hidden");
    resultBox.classList.remove("hidden");
    resultList.innerHTML = "";

    results.forEach((result) => {
        const li = document.createElement("li");
        li.textContent = `题目: ${result.question}, 你的答案: ${questions[result.index].options[result.userAnswer] || "未答"}, 正确答案: ${questions[result.index].options[result.correctAnswer]}`;
        li.classList.add(result.userAnswer === result.correctAnswer ? "correct" : "incorrect");
        resultList.appendChild(li);
    });
}

// 绑定按钮事件
submitButton.addEventListener("click", showResults);
continueButton.addEventListener("click", () => {
    currentIndex += totalQuestionsPerRound;
    if (currentIndex < questions.length) {
        results = [];
        questionBox.classList.remove("hidden");
        resultBox.classList.add("hidden");
        loadQuestions();
    } else {
        alert("题库已完成，感谢您的参与！");
        window.location.href = "../../html/sub-practice/language_test.html";
    }
});

backButton.addEventListener("click", () => {
    window.location.href = "../../html/sub-practice/language_test.html";
});

// 初始化加载
loadQuestions();
